package com.yilin.tms.truck.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.annotation.SkipLoginVerify;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.model.AuthData;
import com.yilin.tms.core.commons.model.AuthStatus;
import com.yilin.tms.core.commons.annotation.Explain;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.annotation.UserTarget;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.truck.TransportLicense;
import com.yilin.tms.core.entity.truck.TravelLicense;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.truck.service.ITransportLicenseService;
import com.yilin.tms.truck.service.ITravelLicenseService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 车辆控制器
 *
 * @author zhangjinyuan
 * @date 2022/11/11
 */
@RestController
@RequestMapping("/truck/auth/")
public class TruckAuthController extends BaseController {

    @Resource
    ITravelLicenseService travelLicenseService;
    @Resource
    ITransportLicenseService transportLicenseService;

    /**
     * 获取车辆认证数据
     */
    @PostMapping(value = "getTruckAuthData")
    @NotNull({"truckId"})
    @UserTarget(UserType.truckerUser)
    public ReturnData<List<AuthData>> getTruckAuthData(@Explain("车辆ID") String truckId) {
        List<AuthData> authOption = new ArrayList<>();
        /// ###### 行驶证
        QueryWrapper<TravelLicense> queryWrapper = new QueryWrapper<TravelLicense>().eq("vehicle_id", truckId);
        AuthData travelLicense = new AuthData(AuthData.AuthItem.authTravelLicense, travelLicenseService.getOne(queryWrapper));
        authOption.add(travelLicense);
        /// ###### 道路运输证
        QueryWrapper<TransportLicense> queryWrapper1 = new QueryWrapper<TransportLicense>().eq("vehicle_id", truckId);
        AuthData transportLicense = new AuthData(AuthData.AuthItem.authTransportLicense, transportLicenseService.getOne(queryWrapper1));
        authOption.add(transportLicense);
        return jsonView(true, "获取车辆认证许可成功！", authOption);
    }

    //-----分页区---------------------------------

    /**
     * 获取行驶证分页
     */
    @PostMapping(value = "getTravelLicensePage")
    @NotNull({"page", "limit"})
    public ReturnData<PageData<TravelLicense>> getTravelLicensePage(TravelLicense query, @Explain("页码") Integer page, @Explain("条数") Integer limit) {
        PageData<TravelLicense> pageData = travelLicenseService.getTravelLicensePage(getLoginUser(), query, page, limit);
        return jsonView(true, "获取认证成功！", pageData);
    }

    /**
     * 获取运输证分页
     */
    @PostMapping(value = "getTransportLicensePage")
    @NotNull({"page", "limit"})
    public ReturnData<PageData<TransportLicense>> getTransportLicensePage(TransportLicense query, @Explain("页码") Integer page, @Explain("条数") Integer limit) {
        PageData<TransportLicense> pageData = transportLicenseService.getTransportLicensePage(getLoginUser(), query, page, limit);
        return jsonView(true, "获取认证成功！", pageData);
    }

    //---获取详情区--------------------

    /**
     * 获取行驶证
     */
    @PostMapping(value = "getTravelLicenseByVehicleId")
    @NotNull({"vehicleId"})
    public ReturnData<TravelLicense> getTravelLicenseByVehicleId(@Explain("车辆ID") String vehicleId) {
        TravelLicense travelLicense = travelLicenseService.getOne(new QueryWrapper<TravelLicense>().eq("vehicle_id", vehicleId));
        return jsonView(true, "获取行驶证成功！", travelLicense);
    }

    /**
     * 获取行驶证2
     */
    @PostMapping(value = "getTravelLicenseByVehicleNo")
    @NotNull({"vehicleNo"})
    public ReturnData<TravelLicense> getTravelLicenseByVehicleNo(@Explain("车牌号") String vehicleNo) {
        TravelLicense travelLicense = travelLicenseService.getOne(new QueryWrapper<TravelLicense>().eq("vehicle_no", vehicleNo));
        return jsonView(true, "获取行驶证成功！", travelLicense);
    }

    /**
     * 获取运输证
     */
    @PostMapping(value = "getTransportLicenseByVehicleId")
    @NotNull({"vehicleId"})
    public ReturnData<TransportLicense> getTransportLicenseByVehicleId(@Explain("车辆ID") String vehicleId) {
        TransportLicense transportLicense = transportLicenseService.getOne(new QueryWrapper<TransportLicense>().eq("vehicle_id", vehicleId));
        return jsonView(true, "获取运输证成功！", transportLicense);
    }

    //---系统审核区--------------------

    /**
     * 审核并更新行驶证数据
     */
    @PostMapping(value = "verifyTravelLicense")
    @NotNull({"truckTypeId", "truckEnergyTypeId"})
    public ReturnData<Object> verifyTravelLicense(TravelLicense travelLicense) {
        travelLicenseService.verifyAndUpdateAuth(getLoginUser(), travelLicense);
        return jsonView(SUCCESS_CODE, "行驶证信息更新成功！");
    }

    /**
     * 审核并更新道路运输证数据
     */
    @PostMapping(value = "verifyTransportLicense")
    @NotNull({"transLicensePhoto", "transLicenseNo", "transLicenseName", "transBusinessNo"})
    public ReturnData<Object> verifyTransportLicense(TransportLicense transportLicense) {
        TravelLicense travelLicense = travelLicenseService.getOne(new QueryWrapper<TravelLicense>().eq("vehicle_id", transportLicense.getVehicleId()));
        ParamUtil.isNull(travelLicense, "请先审核行驶证");
        ParamUtil.notEquals("请先审核行驶证后才能审核运输证", travelLicense.getAuthStatus(), AuthStatus.authSuccess);
        transportLicenseService.verifyAndUpdateAuth(getLoginUser(), transportLicense);
        return jsonView(SUCCESS_CODE, "道路运输证信息更新成功！");
    }


}
